// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Rob a vehicle
// -----------------------------------------------

private [
	"_unit",
	"_startpos",
	"_missioncomplete",
	"_vehicle",
	"_vehicle2",
	"_group"
];

_unit = _this select 0;

_startpos = position _unit;
_missioncomplete = false;

//A3 TODO: setVehicleInit
_unit setDamage floor(0.1 + random 0.7);
_unit setVehicleInit "this setFuel 0;";
processInitCommands;

_group = createGroup east;
_vehicle = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), position _unit, [], 2, "NONE"];
_vehicle2 = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), position _unit, [], 4, "NONE"];

_vehicle allowDammage false;
_vehicle2 allowDammage false;

wcgarbage = [_group, (position (leader _group)), 100] spawn WC_fnc_patrol;
wcgarbage = [_group] spawn WC_fnc_grouphandler;

wcgarbage = [_vehicle] spawn WC_fnc_dosillything;
wcgarbage = [_vehicle2] spawn WC_fnc_dosillything;

sleep 10;

_vehicle allowDammage true;
_vehicle2 allowDammage true;

while {!_missioncomplete} do {
	sleep 1;
	if ((!alive _unit) or (damage _unit > 0.9)) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONFAILED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
	};
	if (_startpos distance position _unit > 1000) then {
		wcmessageW = [localize "STR_WC_MESSAGEMISSIONCOMPLETED", localize "STR_WC_MESSAGELEAVEZONE"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		wcmissionsuccess = true;
		_missioncomplete = true;
		wcleveltoadd = 1;
		wcenemyglobalfuel = wcenemyglobalfuel + wcbonusfuel;
	};
	//A3 TODO: setVehicleInit
	if (isPlayer (driver _unit)) then {
		_unit setVehicleInit "this setFuel 1;";
		processInitCommands;
	};
	if (count (crew _unit) > 0) then {
		{
			if (!(isPlayer _x) and !(side _x in wcside)) then {
				_x action ["eject", _unit];
				unassignVehicle _x;
				(group _x) leaveVehicle _unit;
			};
		} foreach (crew _unit);
	};
};

sleep 120;

_vehicle setDamage 1;
_vehicle2 setDamage 1;
deleteVehicle _vehicle;
deleteVehicle _vehicle2;